package com.wzt.lianfirecontrol.adapter.home.system.baojing;

import android.content.Context;
import android.graphics.drawable.GradientDrawable;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

import androidx.recyclerview.widget.RecyclerView;

import com.wzt.lianfirecontrol.R;
import com.wzt.lianfirecontrol.adapter.BaseRecyclerAdapter;
import com.wzt.lianfirecontrol.bean.recode.home.system.baojing.DetailErrorModel;
import com.wzt.lianfirecontrol.utils.StringUtils;
import com.wzt.lianfirecontrol.utils.Utils;

public class DeviceErrorAdapter extends BaseRecyclerAdapter<DetailErrorModel> {

    private Context context;

    public DeviceErrorAdapter(Context context) {
        this.context = context;
    }

    @Override
    public RecyclerView.ViewHolder onCreate(ViewGroup parent, int viewType) {
        View view = LayoutInflater.from(context).inflate(R.layout.item_baojing_detail_history, parent, false);
        return new MyViewHolder(view);
    }

    @Override
    public void onBind(RecyclerView.ViewHolder viewHolder, int RealPosition, DetailErrorModel data) {
        setItemData((MyViewHolder)viewHolder, data, RealPosition);
    }

    private class MyViewHolder extends RecyclerView.ViewHolder{

        private LinearLayout ll_item_content;
        private TextView tv_title;
        private TextView tv_flag;
        private TextView tv_status;
        private ImageView iv_show_goto;
        private LinearLayout ll_location;
        private ImageView iv_locatioin;
        private TextView tv_location;
        private LinearLayout ll_time;
        private ImageView iv_time;
        private TextView tv_time;

        public MyViewHolder(View itemView) {
            super(itemView);
            initItemView(this, itemView);
        }
    }

    private void initItemView(MyViewHolder myViewHolder, View itemView) {
        myViewHolder.ll_item_content = itemView.findViewById(R.id.ll_item_content);
        myViewHolder.tv_title = itemView.findViewById(R.id.tv_title);
        myViewHolder.tv_flag = itemView.findViewById(R.id.tv_flag);
        myViewHolder.tv_status = itemView.findViewById(R.id.tv_status);
        myViewHolder.iv_show_goto = itemView.findViewById(R.id.iv_show_goto);
        myViewHolder.ll_location = itemView.findViewById(R.id.ll_location);
        myViewHolder.iv_locatioin = itemView.findViewById(R.id.iv_location);
        myViewHolder.tv_location = itemView.findViewById(R.id.tv_location);
        myViewHolder.ll_time = itemView.findViewById(R.id.ll_time);
        myViewHolder.iv_time = itemView.findViewById(R.id.iv_time);
        myViewHolder.tv_time = itemView.findViewById(R.id.tv_time);
    }

    private void setItemData(MyViewHolder viewHolder, DetailErrorModel data, int position) {
        GradientDrawable gradientDrawable = new GradientDrawable();
        gradientDrawable.setColor(context.getResources().getColor(R.color.white));
        RecyclerView.LayoutParams layoutParams = (RecyclerView.LayoutParams) viewHolder.ll_item_content.getLayoutParams();
        if (getmDatas().size() - 1 == position){
            gradientDrawable.setCornerRadii(new float[]{0, 0, 0, 0,
                    Utils.dip2px(context, 5), Utils.dip2px(context, 5),
                    Utils.dip2px(context, 5), Utils.dip2px(context, 5)});
            layoutParams.setMargins(Utils.dip2px(context, 15), 0, Utils.dip2px(context, 15), 0);
        }else {
            gradientDrawable.setCornerRadius(0);
            layoutParams.setMargins(Utils.dip2px(context, 15), 0, Utils.dip2px(context, 15), Utils.dip2px(context, 0.5f));
        }
        viewHolder.ll_item_content.setBackground(gradientDrawable);
        viewHolder.tv_title.setVisibility(View.VISIBLE);
        viewHolder.tv_title.setText("发生报警");

        if (StringUtils.isEmpty(data.getWarnTimeStr())){
            viewHolder.ll_time.setVisibility(View.GONE);
        }else {
            viewHolder.ll_time.setVisibility(View.VISIBLE);
            viewHolder.tv_time.setText(data.getWarnTimeStr());
        }
        viewHolder.iv_show_goto.setVisibility(View.GONE);
    }
}
